package com.meizi.codegenerate.generate.server.domain.repository.dao;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.meizi.codegenerate.constant.JavaClassNameConstant;
import com.meizi.codegenerate.constant.TableNameConstant;
import com.meizi.codegenerate.entity.ProjectPositionEntity;
import com.meizi.codegenerate.entity.TablesEntity;
import com.meizi.codegenerate.packagegenerate.PackageGenerate;
import com.meizi.codegenerate.tool.service.impl.BaseDao;
import com.meizi.codegenerate.utils.AnnotationUtils;
import com.meizi.codegenerate.utils.AttributeConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Mapper;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * @author pc
 * @date 2024/2/6 - 14:40 - 星期二
 * @project lambda
 * @package com.meizi.codegenerate.generate.server.domain.repository.dao
 * @by GG Bomd 猪猪侠
 * mapper接口生成
 */
@Slf4j
public class DaoGenerate {

    /**
     * 生成dao
     * @param tableName
     * @param tablesComment
     * @param projectPosition
     * @param entityTableName
     */
    public static void createDaoGenerate(String tableName, TablesEntity tablesComment, ProjectPositionEntity projectPosition,
                                         String entityTableName) throws IOException {

        // 在相应的路径下创建对应的文件
        String mapperUrl = PackageGenerate.obtainDomainRepositoryDaoUrl(projectPosition);

        //获取 mapper java类名
        String className = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, true)
                + JavaClassNameConstant.MAPPER;

        // 获取完成的文件路径创建文件
        String fullPath = mapperUrl + className + TableNameConstant.JAVA;
        log.info("创建 application 下mapper路径 fullPath = {}", fullPath);

        CompilationUnit compilationUnit = new CompilationUnit();
        // 添加package
        compilationUnit.setPackageDeclaration(PackageGenerate.domainRepositoryDaoPackage(projectPosition));

        // 添加接口文件
        ClassOrInterfaceDeclaration declaration = compilationUnit.addInterface(className).setPublic(true);
        //添加类级别注释
        declaration.setJavadocComment(tablesComment.getTableComment() + " Mapper");

        //获取Mapper java类名
        String MapperClassName = AttributeConvertUtils.tableNameToJava
                (tableName, TableNameConstant.TABLE_PREFIX, true)
                + JavaClassNameConstant.MAPPER;
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();

        // 添加类级别注解
        declaration.addAnnotation(AnnotationUtils.setAnnotation(Mapper.class, new NodeList<>(),
                map));

        // 继承BaseDao
        ClassOrInterfaceType baseDao = new ClassOrInterfaceType(null, BaseDao.class.getSimpleName());
        ClassOrInterfaceType entity = new ClassOrInterfaceType(null, entityTableName);
        baseDao.setTypeArguments(entity);
        declaration.addExtendedType(baseDao);

        log.info("生成 mapper 接口 compilationUnit = {}", compilationUnit.toString());

        /**
         * 把字符串写入文件
         * 参数1：需要写入的文件，如果文件不存在，将自动创建。  参数2：需要写入的内容
         * 参数3：编码格式     参数4：是否为追加模式（ ture: 追加模式，把字符串追加到原内容后面）
         */
        FileUtils.writeStringToFile(new File(fullPath), compilationUnit.toString(), "UTF-8", false);
    }
}
